/**
 * 拖拽组件
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class Dragable extends cc.Component {
    
    @property(cc.Node)
    target: cc.Node = null;
    W:number =0;
    H:number =0;
    start () {
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
        this.W = cc.winSize.width/2;
        this.H = cc.winSize.height/2;

    }
     
    _onTouchMove(event){
        //获取触摸移动增量
        let delta = event.getDelta();
        let node = this.target || this.node;
        //当前节点位置+增量，更新节点位置
        let pos = delta.add(node.position);  
        let W =this.W;
        let H =this.H; 
        if(pos.x > W){
            pos.x = W;
        }
        if(pos.x < -W){
            pos.x = -W;
        }
        if(pos.y > H){
            pos.y = H;
        }
        if(pos.y < -H){
            pos.y = -H;
        }
        //校正位置
        node.position = pos;

    }   
    // update (dt) {}
}
